﻿using WpfModelEditor.Core.Plugins;
using HelixToolkit.Wpf.SharpDX;
using HelixToolkit.Wpf.SharpDX.Model.Scene;
using System.Windows.Controls;

namespace ViewPlugin
{
    /// <summary>
    /// NodePanel.xaml 的交互逻辑
    /// </summary>
    public partial class NodePanel
    {
        private IHost Host { get; set; }
        public NodePanel()
        {
            InitializeComponent();
        }
        public NodePanel(IHost host)
        {
            InitializeComponent();
            Host = host;
        }
        private void AddNode(GroupNode group, TreeViewItem parent)
        {
            foreach (var item in group.Items)
            {
                var treeViewItem = new TreeViewItem();
                treeViewItem.Tag = item;
                treeViewItem.Header = item;
                parent.Items.Add(treeViewItem);

                if (item is GroupNode)
                {
                    AddNode(item as GroupNode, treeViewItem);
                }
            }
        }
        public void Update(SceneNodeGroupModel3D root)
        {
            tree.Items.Clear();
            foreach (var item in root.GroupNode.Items)
            {
                var treeViewItem = new TreeViewItem();
                treeViewItem.Tag = item;
                treeViewItem.Header = item;
                tree.Items.Add(treeViewItem);

                if (item is GroupNode)
                {
                    AddNode(item as GroupNode, treeViewItem);
                }
            }
        }
        public void UpdateSelect()
        {
            var selected = tree.SelectedItem as TreeViewItem;
            if (selected != null && selected.Tag == Host.SelectedObject)
                return;
            if (Host.SelectedObject == null)
                return;
            SelectNode(tree.Items);
        }
        private void Expend(TreeViewItem tvi)
        {
            if (tvi == null)
                return;
            tvi.IsExpanded = true;
            Expend(tvi.Parent as TreeViewItem);
        }
        private bool SelectNode(ItemCollection items)
        {
            foreach (TreeViewItem item in items)
            {
                if (item == null)
                    continue;
                if (item.Tag == Host.SelectedObject)
                {
                    item.IsSelected = true;
                    Expend(item.Parent as TreeViewItem);
                    return true;
                }
                if (SelectNode(item.Items))
                    return true;
            }
            return false;
        }

        private void tree_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (!e.Handled && e.LeftButton == System.Windows.Input.MouseButtonState.Pressed && sender is TreeViewItem)
            {
                var tvi = sender as TreeViewItem;
                var node = tvi.Tag as SceneNode;
                if (node != null)
                {
                    Host.FocusCameraToNode(node);
                }
            }
        }

        private void tree_PreviewMouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs ev)
        {
            if (sender is TreeViewItem)
            {
                var tvi = sender as TreeViewItem;
                var node = tvi.Tag as SceneNode;
                if (node != null)
                {
                    var cm = new ContextMenu();
                    {
                        var mi = new MenuItem
                        {
                            Header = "选中",
                        };
                        mi.Click += (s, e) => Host.SelectedObject = node;
                        cm.Items.Add(mi);
                    }
                    /*
                    {
                        var mi = new MenuItem
                        {
                            Header = node.Visible ? "隐藏" : "显示",
                        };
                        mi.Click += (s, e) => node.Visible = !node.Visible;
                        cm.Items.Add(mi);
                    }
                    if (node is MeshNode)
                    {
                        var mn = node as MeshNode;
                        var mi = new MenuItem
                        {
                            Header = mn.FillMode == SharpDX.Direct3D11.FillMode.Solid ? "网格" : "实体",
                        };
                        mi.Click += (s, e) => mn.FillMode = mn.FillMode == SharpDX.Direct3D11.FillMode.Solid ? SharpDX.Direct3D11.FillMode.Wireframe : SharpDX.Direct3D11.FillMode.Solid;
                        cm.Items.Add(mi);
                    }*/
                    tvi.ContextMenu = cm;
                }
                else
                {
                    tvi.ContextMenu = null;
                }
            }
        }
    }
}
